package com.sogou.adm.bizdev.dataimport.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.sogou.adm.bizdev.dataimport.service.JobManager;
import com.sogou.adm.bizdev.dataimport.vo.DataJobDetail;

@Results( { @Result(name = CrudActionSupport.RELOAD, location = "job.action", type = "redirect") })
public class JobAction extends CrudActionSupport<DataJobDetail>  {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8506376929268104466L;
	@Autowired
	private JobManager jobManager;
	private String name;
	private String group;

	private Page<DataJobDetail> page = new Page<DataJobDetail>(5);
	private DataJobDetail entity;

	public Page<DataJobDetail> getPage() {
		return page;
	}

	
	@Override
	protected void prepareModel() throws Exception {
		if (name != null) {
			entity = jobManager.getJob(name, group);
		} else {
			entity = new DataJobDetail();
		}
	}

	@Override
	public String execute() throws Exception {
		return list();
	}

	@Override
	public String list() throws SchedulerException {
		List<DataJobDetail> result = jobManager.getAll();
		page.setTotalCount(result.size());
		page.setResult(result);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {

		jobManager.addJob(entity);
		addActionMessage("保存任务成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		jobManager.deleteJob(name, group);
		addActionMessage("删除任务成功");
		return RELOAD;
	}


	public DataJobDetail getModel() {
		return entity;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}
}
